﻿namespace SocketServer.SocketResult
{
    /// <summary>
    /// Socket以Json的方式输出给前端
    /// </summary>
    public class SocketJsonResult : ISocketResult
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Data">返回数据</param>
        /// <param name="Settings">序列化规则</param>
        public SocketJsonResult(object Data, Newtonsoft.Json.JsonSerializerSettings Settings = null)
        {
            this.Data = Data;
            this.Settings = Settings ?? DefaultTimeSettings;
        }

        /// <summary>
        /// 返回数据
        /// </summary>
        private object Data { get; }

        /// <summary>
        /// 返回数据
        /// </summary>
        private Newtonsoft.Json.JsonSerializerSettings Settings { get; }

        /// <summary>
        /// 默认序列化规则(处理时间格式中的T)
        /// </summary>
        public static readonly Newtonsoft.Json.JsonSerializerSettings DefaultTimeSettings = new Newtonsoft.Json.JsonSerializerSettings
        {
            DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
            DateFormatString = "yyyy-MM-dd HH:mm:ss",
            //NullValueHandling = NullValueHandling.Ignore,
        };

        /// <summary>
        /// Socket方法项前端输出数据的方式
        /// </summary>
        /// <param name="webSocket">webSocket链接</param>
        public void SendResult(Fleck.IWebSocketConnection webSocket)
        {
            webSocket.Send(Newtonsoft.Json.JsonConvert.SerializeObject(Data, Settings));
        }
        /// <inheritdoc/>
        public object GetData()
        {
            return Data;
        }
    }
}
